// package/pages/swapCard/swapCard.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneTime: 60,
    phoneCodeShow: true,
    smsCode: '',
  },
  //获取手机号验证码
  phoneCode() {
    const that = this;
    if (that.data.phone.length == 11) {
      let data = {
        appid: my.getAppIdSync().appId,
        phone: that.data.phone,
        cardnumber: '',
        openId: my.getStorageSync({
          key: 'openId'
        }).data,
      }
      my.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.AlipayMemberCardBindSendSMSCode, data).then(res => {
        my.hideLoading()
        if (res.data.data.success == true) {
          my.showToast({
            content: '验证码发送成功',
            type: 'success',
          })
          that.setData({
            phoneCodeShow: false,
          })
          let time = setInterval((() => {
            if (that.data.phoneTime == 1) {
              clearInterval(time)
              that.setData({
                phoneCodeShow: true,
                phoneTime: 60
              })
            } else {
              that.setData({
                phoneCodeShow: false,
                phoneTime: that.data.phoneTime - 1
              })
            }
          }), 1000)
        } else {
          my.showModal({
            showCancel: false,
            content: res.data.data.msg,
            success(res) {
              if (res.confirm) {

              }
            }
          })
        }
      }).catch(err => {
        console.log(err, 888)
      })
    } else {
      my.showToast({
        content: '手机号格式错误',
        type: 'exception',
      })
    }


  },
  getPhoneCode(e) {
    const that = this;
    that.setData({
      smsCode: e.detail.value
    })
  },
  getPhone(e) {
    const that = this;
    that.setData({
      phone: e.detail.value,
    })
  },
  //手机号验证
  bindBySms() {
    const that = this;
    if (!that.data.phone) {
      my.showToast({
        content: '请输入手机号码',
        type: 'exception',

      })
      return
    }
    if (!that.data.smsCode) {
      my.showToast({
        content: '请输入验证码',
        type: 'exception',

      })
      return
    }
    let data = {
      appid: my.getAppIdSync().appId,
      openId: my.getStorageSync({
        key: 'openId'
      }).data,
      phone: that.data.phone,
      checkcode: that.data.smsCode,
    }
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.AlipayMemberCardBindCheckSMSCode, data).then(res => {
      my.hideLoading()
      console.log(res, 898989)
      if (res.data.data.success == true) {
        that.setData({
          phoneCodeShow: true,
          phoneTime: 60
        })
        let stringArray = res.data.data.cards.map(item => `${item.cardnumber} 余额￥${item.totalamount}`);
        // 弹出选择卡号对话框
        my.optionsSelect({
          title: "选择绑定卡号",
          optionsOne: stringArray,
          positiveString: "立即绑定",
          success(prop) {
            console.log(prop, 9999)
            if (prop.selectedOneIndex !== "") {
              console.log(123)
              let cardnumber = res.data.data.cards[prop.selectedOneIndex].cardnumber
              that.bindCardNumber(cardnumber)
            }
          }
        });
      } else {
        my.showModal({
          showCancel: false,
          content: res.data.data.msg,
          success(res) {
            if (res.confirm) {

            }
          }
        })
      }
    })
  },
  bindCardNumber(cardNumber) {
    const that = this;
    let data = {
      appid: my.getAppIdSync().appId,
      openId: my.getStorageSync({
        key: 'openId'
      }).data,
      phone: that.data.phone,
      cardnumber: cardNumber,
      checkcode: that.data.smsCode,
      way: 1,
    }
    myRequest.EasyHttp.post(api.reqUrls.alipayMemberCardBind, data).then(res => {
      my.hideLoading();
      console.log(res, 787878)
      if (res.data.data.success) {
       that.getCardinfoData()
        my.showModal({
          content: '绑卡成功',
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {
            my.navigateBack()
          }
        })
      } else {
        my.showToast({
          content: res.data.data.msg,
          type: 'exception',
        })
      }
    })
  },
   //获取会员信息
   getCardinfoData() {
    const that = this;
    myRequest.EasyHttp.post(api.reqUrls.membercardShow).then(res => {
      if (res.data.status == 1) {
        my.setStorageSync({
          key: 'isVip',
          data: true
        })
        my.setStorageSync({
          key: 'refresh',
          data: false
        })
        my.setStorageSync({
          key: 'isfoundingmember',
          data: res.data.result.member_data.isfoundingmember
        })
        my.setStorageSync({
          key: 'cardnumber',
          data: res.data.result.member_data.cardnumber
        })
        my.setStorageSync({
          key: 'totalamount',
          data: res.data.result.member_data.totalamount
        })
        my.setStorageSync({
          key: 'userName',
          data: res.data.result.member_data.name
        })
        my.setStorageSync({
          key: 'userPhone',
          data: res.data.result.member_data.phone
        })
      } else if (res.data.status == -2) {
        my.setStorageSync({
          key: 'isVip',
          data: false
        })
        my.setStorageSync({
          key: 'refresh',
          data: false
        })
        my.setStorageSync({
          key: 'isfoundingmember',
          data: false
        })
        my.setStorageSync({
          key: 'cardnumber',
          data: ''
        })
        my.setStorageSync({
          key: 'totalamount',
          data: 0
        })
        my.setStorageSync({
          key: 'userName',
          data: ''
        })
        my.setStorageSync({
          key: 'userPhone',
          data: ''
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})